package com.CST.service.impl.zx;

import com.CST.bo.zx.ProjectAllBO;
import com.CST.bo.zx.ProjectDetailBO;
import com.CST.common.api.ResultObject;
import com.CST.entity.zx.ZxSumMoney;
import com.CST.entity.zx.ZxUserProjectRelation;
import com.CST.mapper.cst.zx.ZxSumMoneyMapper;
import com.CST.mapper.cst.zx.ZxUserProjectRelationMapper;
import com.CST.service.zx.IZxSumMoneyService;
import com.CST.util.ExcelTransfer;
import com.CST.util.ProjectTimeUtil;
import com.CST.vo.cb.ExcelHead;
import com.CST.vo.cb.ExcelVO;
import com.CST.vo.zx.ProjectAllVO;
import com.CST.vo.zx.ProjectDetailVO;
import com.CST.vo.zx.ZxFinicialDetailVO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.time.temporal.TemporalAdjusters;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ly
 * @since 2023-03-14
 */
@Service
public class ZxSumMoneyServiceImpl extends ServiceImpl<ZxSumMoneyMapper, ZxSumMoney> implements IZxSumMoneyService {

    @Resource
    private ZxSumMoneyMapper zxSumMoneyMapper;
    @Resource
    private ExcelTransfer excelTransfer;
    @Autowired
    private ZxUserProjectRelationMapper zxUserProjectRelationMapper;
    @Override
    public ResultObject insertBaseData(Integer date) {
        LambdaQueryWrapper<ZxSumMoney> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(date != null,ZxSumMoney::getStartPeriod,date);
        List<ZxSumMoney> list = null;
        list = zxSumMoneyMapper.selectList(lambdaQueryWrapper);
        if (list.size() != 0){
            return ResultObject.success("数据导入成功");
        }

        list = zxSumMoneyMapper.queryBaseMapper(date);

//        return ResultObject.success(list);
        return zxSumMoneyMapper.saveBaseBatch(list)?ResultObject.success("导入成功"):ResultObject.failed("导入失败");
    }

    @Override
    public List<ProjectDetailVO> queryAllProjectDetail(ProjectDetailBO projectDetailBO) {
        List<ProjectDetailVO> list = zxSumMoneyMapper.selectAllProjectDetail(projectDetailBO);
        return list;
    }

    @Override
    public List<ProjectAllVO> queryAllProject(ProjectAllBO projectAllBO, HttpServletRequest request) {
        String userId = request.getHeader("userName");
        Map<String, Integer> map = ProjectTimeUtil.operateTime(projectAllBO.getStartTime(), projectAllBO.getEndTime());
        Integer startPeriod = map.get("startPeriod");
        Integer endPeriod = map.get("endPeriod");
        projectAllBO.setStartPeriod(startPeriod);
        projectAllBO.setEndPeriod(endPeriod);
        //获取开始时间的下个月的第一天
        projectAllBO.setStartTime(projectAllBO.getStartTime().with(TemporalAdjusters.firstDayOfNextMonth()));
        //获取结束时间的下个月的第一天
        projectAllBO.setEndTime(projectAllBO.getEndTime().with(TemporalAdjusters.firstDayOfNextMonth()));
        List<ZxUserProjectRelation> vos = zxUserProjectRelationMapper.inList(userId);
        //用户权限配置的对应项目类别id
        List<String> entryCodeList = vos.stream().map(ZxUserProjectRelation::getEntryCode).collect(Collectors.toList());
        List<ProjectAllVO> list = zxSumMoneyMapper.selectAllProject(projectAllBO);
        list = list.stream().filter(t -> entryCodeList.contains(t.getProjectCategoryId())).collect(Collectors.toList());
        return list;
    }

    @Override
    public ZxFinicialDetailVO queryFinicialDetail(ProjectDetailBO projectDetailBO) {
        return zxSumMoneyMapper.queryFinicialDetail(projectDetailBO);
    }

    @Override
    public void downloadFile(ProjectAllBO projectAllBO, HttpServletResponse response, HttpServletRequest request) {
        ExcelTransfer<ProjectAllVO> excelTransfer = new ExcelTransfer<>();
        List<ProjectAllVO> projectAllVOS = this.queryAllProject(projectAllBO,request);
        try {
            excelTransfer.exportExcel(response,projectAllVOS,"项目档案","项目档案详情", ProjectAllVO.class);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public void downloadDetailFile(ProjectDetailBO projectDetailBO, HttpServletResponse response) throws IOException {
        List<ExcelVO> voList = new ArrayList<>();
        List<String> excelHeads = new ArrayList<>();
        excelHeads.add("财务编码");
        excelHeads.add("项目编号");
        excelHeads.add("项目负责人");
        excelHeads.add("项目简称");
        excelHeads.add("项目全称");
        excelHeads.add("期初金额");
        excelHeads.add("项目经费");
        excelHeads.add("支出总额");
        excelHeads.add("项目余额");
        excelHeads.add("收入");
        excelHeads.add("设备费");
        excelHeads.add("会议费");
        excelHeads.add("人工费");
        excelHeads.add("差旅费");
        excelHeads.add("材料费");
        excelHeads.add("其他支出");
        excelHeads.add("劳务费");
        excelHeads.add("出版/文献/信息传播/知识产权");
        List<List<String>> head = this.getStringHead(excelHeads);
        ZxFinicialDetailVO zxFinicialDetailVO = this.queryFinicialDetail(projectDetailBO);



        List<List<Object>> data = new ArrayList<>();
        List<Object> objects = new ArrayList<>();
        objects.add(zxFinicialDetailVO.getFinancialCode());
        objects.add(zxFinicialDetailVO.getProjectCode());
        objects.add(zxFinicialDetailVO.getUserName());
        objects.add(zxFinicialDetailVO.getEasyProName());
        objects.add(zxFinicialDetailVO.getProAllName());
        objects.add(zxFinicialDetailVO.getStartMoney());
        objects.add(zxFinicialDetailVO.getProjectMoney());
        objects.add(zxFinicialDetailVO.getPayAllMoney());
        objects.add(zxFinicialDetailVO.getSurplusMoney());
        objects.add(zxFinicialDetailVO.getIncome());
        objects.add(zxFinicialDetailVO.getEquipmentMoney());
        objects.add(zxFinicialDetailVO.getMeetingMoney());
        objects.add(zxFinicialDetailVO.getPeopleMoney());
        objects.add(zxFinicialDetailVO.getTravelMoney());
        objects.add(zxFinicialDetailVO.getMaterialMoney());
        objects.add(zxFinicialDetailVO.getElseMoney());
        objects.add(zxFinicialDetailVO.getLabourServiceMoney());
        objects.add(zxFinicialDetailVO.getKnowMoney());
        data.add(objects);


        List<String> excelHeads1 = new ArrayList<>();
        excelHeads1.add("发生时间");
        excelHeads1.add("凭证号数");
        excelHeads1.add("科目编码");
        excelHeads1.add("科目名称");
        excelHeads1.add("费用类别编号");
        excelHeads1.add("费用类型名称");
        excelHeads1.add("收支类型编号");
        excelHeads1.add("收支类型名称");
        excelHeads1.add("摘要");
        excelHeads1.add("余额");
        List<List<String>> head1 = this.getStringHead(excelHeads1);


        List<ProjectDetailVO> list = this.queryAllProjectDetail(projectDetailBO);
        List<List<Object>> data1 = new ArrayList<>();
        list.forEach(projectDetailVO -> {
            List<Object> list1 = new ArrayList<>();
            list1.add(projectDetailVO.getOperDtime());
            list1.add(projectDetailVO.getAccountNum());
            list1.add(projectDetailVO.getAccountCode());
            list1.add(projectDetailVO.getAccountName());
            list1.add(projectDetailVO.getExpenseTypeCode());
            list1.add(projectDetailVO.getExpenseTypeName());
            list1.add(projectDetailVO.getReExTypeCode());
            list1.add(projectDetailVO.getReExTypeName());
            list1.add(projectDetailVO.getDescription());
            list1.add(projectDetailVO.getBalance());
            data1.add(list1);
        });

        voList.add(ExcelVO.builder().sheet("页面上层数据").listsHead(head).listsData(data).build());
        voList.add(ExcelVO.builder().sheet("页面下层数据").listsHead(head1).listsData(data1).build());
//                l(excelHeads).listsData(otherData).build());
        excelTransfer.exportExcel(response,"项目收支明细",voList);
    }

    private List<List<String>> getStringHead(List<String> old) {
        List<List<String>> head = new ArrayList<>();
        old.forEach(e -> {
            List<String> list = new ArrayList<>();
            list.add(e);
            head.add(list);
        });
        return head;
    }

    private List<List<String>> getHead(List<ExcelHead> headList) {
        List<List<String>> lists = new ArrayList<>();
        headList.forEach(e -> e.getCHead().forEach(c -> {
            List<String> list = new ArrayList<>();
            list.add(e.getHead());
            list.add(c);
            lists.add(list);
        }));
        return lists;
    }
}
